package com.gitee.wsl.collections.iterator

import com.gitee.wsl.base.Objects


/**
 * Decorates another iterator to return elements in a specific range.
 *
 *
 * The decorated iterator is bounded in the range [offset, offset+max).
 * The `offset` corresponds to the position of the first element to
 * be returned from the decorated iterator, and `max` is the maximum
 * number of elements to be returned at most.
 *
 *
 *
 * In case an offset parameter other than 0 is provided, the decorated
 * iterator is immediately advanced to this position, skipping all elements
 * before that position.
 *
 *
 * @param <E> the type of elements returned by this iterator.
 * @since 4.1
</E> */
class BoundedIterator<E>(private val iterator: Iterator<E>, offset: Long, max: Long) : Iterator<E> {
    /** The iterator being decorated.  */
    //private val iterator: Iterator<out E?>

    /** The offset to bound the first element return  */
    private val offset: Long

    /** The max number of elements to return  */
    private val max: Long

    /** The position of the current element  */
    private var pos: Long

    /**
     * Decorates the specified iterator to return at most the given number of elements,
     * skipping all elements until the iterator reaches the position at `offset`.
     *
     *
     * The iterator is immediately advanced until it reaches the position at `offset`,
     * incurring O(n) time.
     *
     *
     * @param iterator  the iterator to be decorated
     * @param offset  the index of the first element of the decorated iterator to return
     * @param max  the maximum number of elements of the decorated iterator to return
     * @throws NullPointerException if iterator is null
     * @throws IllegalArgumentException if either offset or max is negative
     */
    init {
        require(offset >= 0) { "Offset parameter must not be negative." }
        require(max >= 0) { "Max parameter must not be negative." }

        this.offset = offset
        this.max = max
        pos = 0
        init()
    }

    /**
     * Checks whether the iterator is still within its bounded range.
     * @return `true` if the iterator is within its bounds, `false` otherwise
     */
    private fun checkBounds(): Boolean {
        if (pos - offset + 1 > max) {
            return false
        }
        return true
    }

    public override fun hasNext(): Boolean {
        if (!checkBounds()) {
            return false
        }
        return iterator.hasNext()
    }

    /**
     * Advances the underlying iterator to the beginning of the bounded range.
     */
    private fun init() {
        while (pos < offset && iterator.hasNext()) {
            iterator.next()
            pos++
        }
    }

     override fun next(): E {
        if (!checkBounds()) {
            throw NoSuchElementException()
        }
        val next = iterator.next()
        pos++
        return next
    }

    /**
     * {@inheritDoc}
     *
     *
     * In case an offset other than 0 was specified, the underlying iterator will be advanced
     * to this position upon creation. A call to [.remove] will still result in an
     * [IllegalStateException] if no explicit call to [.next] has been made prior
     * to calling [.remove].
     *
     */
//    public override fun remove() {
//        check(pos > offset) { "remove() cannot be called before calling next()" }
//        iterator.remove()
//    }
}
